﻿using System;
using System.Collections.Generic;
using System.Linq;
using CheatMenu.Managers;

namespace CheatMenu.Utils
{
	public class CommandGroup
	{
		public string Name { get; set; }

		public List<Command> Commands { get; private set; } = new List<Command>();

		public CommandGroup(string name)
		{
			this.Name = name;
		}

		public void ClearCommands()
		{
			this.Commands.Clear();
		}

		public void AddCommand(string name, Action<Command> action, bool toggleState = false)
		{
			if ((from c in this.Commands
				 where c.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
				 select c).Count<Command>() == 0)
			{
				this.Commands.Add(new Command(name, action, toggleState));
				return;
			}
			LogManager.LogWarning("Trying to add existing command with name: " + name);
		}

		public void RemoveCommand(string name)
		{
			IEnumerable<Command> source = from c in this.Commands
										  where c.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
										  select c;
			if (source.Count<Command>() == 1)
			{
				this.Commands.Remove(source.First<Command>());
				return;
			}
			LogManager.LogWarning("Trying to remove non-existing command with name: " + name);
		}
	}
}
